﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace UdrinkWater_Project.Officer
{
    public partial class OutStock : System.Web.UI.Page
    {
        private string Connection = ConfigurationManager.ConnectionStrings["DB_Udrink"].ConnectionString;
        protected void Page_Load(object sender, EventArgs e)
        {
            DataTable dtOutList = GetOUTList();
            gv1.DataSource = dtOutList;
            gv1.DataBind();
        }
        private DataTable GetOUTList()
        {
            string sqltext = @"SELECT DISTINCT TB_StockProductOUT.Product_Date, TB_StockProductOUT.Act_Officer, 
            TB_Employee.Prefix + ' ' + TB_Employee.Firstname + ' ' + TB_Employee.Lastname AS EmpName 
            FROM TB_Product LEFT OUTER JOIN TB_Unit_Category ON TB_Product.Unit = TB_Unit_Category.Unit_ID
            LEFT OUTER JOIN TB_StockProductOUT LEFT OUTER JOIN TB_Employee ON TB_StockProductOUT.Act_Officer = TB_Employee.ID_Card ON TB_Product.Product_ID = TB_StockProductOUT.Product_ID";
            SqlConnection sqlconn = new SqlConnection(Connection);
            SqlDataAdapter da = new SqlDataAdapter(sqltext, sqlconn);
            DataSet ds = new DataSet();
            da.Fill(ds);
            return ds.Tables[0];
        }

        protected void ASPxGridView1_RowCommand(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewRowCommandEventArgs e)
        {
            if (e.CommandArgs.CommandName == "Cmd_Select")
            {
                string Id = ChangeYear(e.CommandArgs.CommandArgument.ToString());
                Session["ProductDate"] = Id;
                Response.Redirect("OutStockDetial.aspx");

            }
        }
        private string ChangeYear(string InDate)
        {
            string day = "";
            string mount = "";
            string years = "";
            try
            {
                try
                {
                    string[] stemp = InDate.Split('-');
                    years = (stemp[0]);
                    mount = (stemp[1]);
                    day = (stemp[2]);


                }
                catch (Exception)
                {

                    string[] stemp = InDate.Split('/');
                    years = (stemp[0]);
                    mount = (stemp[1]);
                    day = (stemp[2]);

                }


                if (int.Parse(years) > 2300)
                {
                    years = (int.Parse(years) - 543).ToString();
                }
                return (years + "-" + mount + "-" + day);
            }
            catch (Exception)
            {

            }

            return (years + "-" + mount + "-" + day);
        }
    }
}